<!--
 * @Description: 月计划调整
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-06-07 14:28:34
 * @LastEditTime: 2023-07-13 11:27:21
 * @FilePath: \cscec-pms-admin\src\views\construction\monthPlanAdjust\index.vue
-->

<template>
  <base-breadcrumb>
    <a-row
      :gutter="10"
      class="page-height"
    >
      <!-- 树搜索 -->
      <a-col
        :span="6"
        class="permission-collapsed"
        :class="{ 'permission-tree-collapsed': collapsed }"
      >
        <a-card
          :style="{ border: collapsed ? 'none' : 'border: 1px solid #e8e8e8' }"
          class="page-height-tree"
        >
          <title-name title="组织架构" />
          <PermissionTreeOrganization
            :collapsed.sync="collapsed"
            @select="handleSelect"
          />
        </a-card>
      </a-col>
      <!-- 树搜索 -->
      <!-- 列表 -->
      <a-col
        :span="18"
        class="permission-collapsed page-height-list-wrapper"
        :style="collapsed ? 'width:calc(100% - 10px)' : ''"
      >
        <searchData
          :search-data="searchOption"
          :custom-reset="customReset"
          @search="search"
          @loadMonth="loadMonth"
        />
        <a-card class="page-height-list">
          <div class="table-operator">
            <a-button
              v-btnPermission="'pm_069_add_02'"
              type="primary"
              @click="handleAdd()"
            >
              新增调整
            </a-button>
          </div>
          <div class="pub-table-select-count">
            <a-icon
              type="info-circle"
              theme="twoTone"
            />
            <span>
              月计划调整：<span class="totalNum">{{ statistics.totalNum }}</span>
            </span>
            <span>
              待提交：<span class="totalNum">{{ statistics.draftNum }}</span>
            </span>
            <span>
              审核中：<span class="totalNum">{{ statistics.submittedNum }}</span>
            </span>
            <span>
              审核不通过：<span class="totalNum">{{ statistics.rejectedNum	}}</span>
            </span>
            <span>
              审核通过：<span class="totalNum">{{ statistics.pastNum }}</span>
            </span>
          </div>
          <a-spin :spinning="loading">
            <list-table
              ref="table"
              :columns="columns"
              :request="loadData"
              :page-keys=" ['pageNum', 'pageSize', 'totalSize']"
              :is-show-total="false"
            >
              <template
                slot="operation"
                slot-scope="text,record"
              >
                <MoreButton>
                  <a
                    v-btnPermission="'pm_069_loo_01'"
                    @click="handleDetail(record)"
                  >查看</a>
                  <a
                    v-if="record.auditStatus == 0 || record.auditStatus == 3"
                    v-btnPermission="'pm_069_edi_03'"
                    @click="handleUpdate(record)"
                  >编辑</a>
                  <a
                    v-if="(record.auditStatus == 0 || record.auditStatus == 3) && hideBuquBtn()"
                    style="color:red"
                    @click="handleDelete(record)"
                  >删除</a>
                  <a
                    v-if="record.auditStatus != 0"
                    @click="$bus.$emit('showApprovalDetailsDialog', record.id)"
                  >审批信息</a>
                </MoreButton>
              </template>
            </list-table>
          </a-spin>
        </a-card>
      </a-col>
      <!-- 列表 -->
    </a-row>
  </base-breadcrumb>
</template>
<script>
import { searchOption, columns } from './model.js'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import MoreButton from '@/components/MoreButton'
import { list } from '@/api/construction/monthPlanAdjust'
export default {
  name: 'monthPlanAdjust' + Date.now(),
  components: {
    PermissionTreeOrganization,
    MoreButton
  },
  data() {
    return {
      // 收缩展开树
      collapsed: false,
      // 树选中项
      select: {},
      // 树初始化完成
      treeReady: false,
      // 查询参数
      searchData: {},
      // 搜索选项
      searchOption,
      // 加载
      loading: false,
      //表头
      columns,
      // 统计
      statistics: {},
      // 所属月默认值
      belongMonth: '',
      // 树所选节点下所有末级节点集合
      projectSet: [],
      // 表格数据加载
      loadData: async (q) => {
        try {
          if (!this.treeReady) {
            return
          }
          // 树选中
          if (Object.keys(this.select).length > 0) {
            if ((this.searchData?.belongMonth ?? '') === '') {
              return Promise.resolve({
                data: {}
              })
            }
            const searchData = Object.assign(q, { param: { ...this.searchData, searchQuery: { projectSet: this.projectSet } } })
            this.getStatistics({ ...this.searchData, searchQuery: { projectSet: this.projectSet } })
            const res = await list.getMonthPlanList(searchData)
            if (res.code === 200) {
              return { 
                code: 200,
                data: res.data,
                totalSize: res.data.total
              }
            } else {
              return {
                code: 200,
                data: {}
              }
            }
          } else {
            this.$message.warning('请选择组织架构')
            return { code: 200, data: {} }
          }
        } catch (error) {
          console.log(error)
          return { code: 200, data: {} }
        }
      }
    }
  },
  methods: {
    /**
     * @description: 树选中
     * @param {object} node 节点对象
     * @return {void}
     */
    handleSelect(node) {
      this.select = node
      if (!this.treeReady) this.treeReady = true
      const list = []
      const flat = (tree) => {
        for (let i = 0; i < tree.length; i++) {
          if (tree[i].tageCode === 'PD') {
            list.push(tree[i].projectId)
          }
          if (Array.isArray(tree[i].childTree) && tree[i].childTree.length > 0) {
            flat(tree[i].childTree)
          }
        }
      }
      flat([node])
      this.projectSet = list
      this.$refs.table.getData(null, true)
    },
    /**
     * @description: 搜索
     * @param {object} value 搜索数据
     * @return {void}
     */
    search(value) {
      if (!this.treeReady) {
        this.$message.warning('请等待组织架构加载完成')
        return
      }
      this.searchData = value || {}
      if ((this.searchData?.belongMonth ?? '') === '') {
        this.$message.warning('缺少所属月')
      }
      this.$refs.table.getData(null, true)
    },
    /**
     * @description: 自定义重置
     * @param {object} form 表单对象
     * @return {void}
     */
    customReset(form) {
      form.setFieldsValue({ belongMonth: this.belongMonth })
    },
    /**
     * @description: 所属月首次加载完成且有默认值回调
     * @param {string} val 默认值
     * @return {void}
     */
    loadMonth(val) {
      this.searchData = {
        ...this.searchData,
        belongMonth: val
      }
      this.belongMonth = val
    },
    /**
     * @description:
     * @param {object} searchData 查询参数
     * @return {void}
     */
    getStatistics(searchData) {
      list.getMonthPlanStatistics(searchData).then(({ code, data }) => {
        if (code === 200) {
          this.statistics = data
          const { draftNum, submittedNum, rejectedNum, pastNum } = data
          this.statistics.totalNum = draftNum + submittedNum + rejectedNum + pastNum
        }
      })
    },
    /**
     * @description: 新增调整
     * @param {void}
     * @return {void}
     */
    handleAdd() {
      this.$router.push({
        path: '/construction/monthPlanAdjust/monthPlanAdjustView',
        query: {
          pageType: 'add'
        }
      })
    },
    /**
     * @description: 查看
     * @param {object} record
     * @return {void}
     */
    handleDetail(record) {
      this.$router.push({
        path: '/construction/monthPlanAdjust/monthPlanAdjustView',
        query: {
          pageType: 'detail',
          id: record.id
        }
      })
    },
    /**
     * @description: 编辑
     * @param {object} record
     * @return {void}
     */
    handleUpdate(record) {
      this.$router.push({
        path: '/construction/monthPlanAdjust/monthPlanAdjustView',
        query: {
          pageType: 'update',
          id: record.id
        }
      })
    },
    /**
     * @description: 删除
     * @param {object} record
     * @return {void}
     */
    handleDelete(record) {
      const vm = this
      this.$confirm({
        title: '是否删除当前数据？',
        okText: '确定',
        cancelText: '取消',
        okType: 'danger',
        onOk() {
          vm.loading = true
          list
            .deleteBasis({ id: record.id })
            .then(({ code }) => {
              if (code === 200) {
                vm.$message.success('删除成功')
                vm.$refs.table.getData(null, true)
              }
            })
            .finally(() => {
              vm.loading = false
            })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.pub-table-select-count {
  > span {
    margin-right: 12px;
  }
}
</style>


